Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is preferred size ignored in QMainWindow (Qt5)?

Why is preferred size ignored in QMainWindow (Qt5)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 528 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • idlefrogI Offline
    idlefrogI Offline
    idlefrog
    wrote on last edited by idlefrog
    #1

    I created a simple default project from QtCreator (without a form).
    Then I change just QMainWindow. I set the size policy to minimum (vertical & horizonal).
    Then I added a QLineEdit to the window, and set its vertical policy to minimum.
    So the question is, why does the code below show a window that's taller than the edit bar?
    96a2cdff-20c3-4208-aa8c-1f6e76b746c1-image.png ![alt text](image url)

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QSizePolicy policy = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        setSizePolicy(policy);
    
        QWidget     *widget = new QWidget();
        QVBoxLayout *layout = new QVBoxLayout();
        QLineEdit   *edit   = new QLineEdit;
    
        policy = QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        edit->setSizePolicy(policy);
        edit->setGeometry(0,0, 96,56);
    
        layout->addWidget(edit);
        widget->setLayout(layout);
    
        setCentralWidget(widget);
    }
    

    What am I missing? Why doesn't it look like this below ? :
    42bb22bf-2856-4f47-9e00-36cd20b18bc3-image.png

    Of course, if I used QWidget instead of QMainWindow, it would look fine, but what do I need to set in QMainWindow for it not to pad out space vertically around the label?
    Alternatively, I just add the call resize(sizeHint()); inside QMainWindow's constructor (at the end)?
    This works, but it seems a bit clumsy.

    1 Reply Last reply
    0
    • MitsuriouM Offline
      MitsuriouM Offline
      Mitsuriou
      wrote on last edited by
      #2

      Hello,

      I'm not sure these are the best practises but please try to:

      • Remove the calls related to the size policy. The window will be taking the less needed space, by default, If I'm not mistaken.
      • And, or, remove the explicit call to setGeometry() to let default values being applied;
      • And, or, call adjustSize() just before calling show().
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved